package com.coincalf.admin.web.funds.trading;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.coincalf.admin.annotation.Log;
import com.coincalf.admin.common.ResultMap;
import com.coincalf.admin.entity.EntrustOrder;
import com.coincalf.admin.enums.SysLogTypeEnum;
import com.coincalf.admin.service.EntrustOrderService;
import com.coincalf.admin.view.CsvView;
import com.coincalf.admin.view.ReportCsvUtils;
import com.google.common.base.Strings;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.supercsv.cellprocessor.CellProcessorAdaptor;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.util.CsvContext;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 委托订单信息_创新交易 前端控制器
 * </p>
 *
 * @author Haliyo
 * @since 2018-05-13
 */
@RestController
@RequestMapping("/forexEntrustOrder")
@Api(value = "委托订单_创新交易", description = "创新交易委托订单管理")
public class ForexEntrustOrderController {

    @Autowired
    private EntrustOrderService entrustOrderService;

    @Log(value = "查询委托订单_创新交易列表",type = SysLogTypeEnum.SELECT)
    @GetMapping
    @ApiImplicitParams({
            @ApiImplicitParam(name = "size", value = "每页显示条数，默认 10", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "marketId", value = "交易市场ID", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "交易方式(1买 2卖)", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "priceType", value = "价格类型(1 市价 2 限价)", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderType", value = "订单类型(1 开仓 2 平仓)", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "交易状态(0未成交 1已成交 2已取消 4异常单)", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "订单ID", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userName", value = "用户名", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String", paramType = "query"),
    })
    @ApiOperation(authorizations = {@Authorization(value="Authorization")},value = "按条件分页查询委托订单_创新交易列表" ,httpMethod = "GET")
    public Object selectPage(@ApiIgnore Page<EntrustOrder> page,
                             String marketId, String type,
                             String priceType, String orderType,
                             String status, String id,
                             String userId,String userName,
                             String mobile,String startTime,
                             String endTime) {
        EntityWrapper<EntrustOrder> ew = new EntityWrapper<>();
        ew.eq("market_type",2);
        if (!Strings.isNullOrEmpty(marketId)) {
            ew.eq("market_id", marketId);
        }
        if(!Strings.isNullOrEmpty(type)){
            ew.eq("a.type", type);
        }
        if(!Strings.isNullOrEmpty(priceType)){
            ew.eq("a.price_type", priceType);
        }
        if(!Strings.isNullOrEmpty(orderType)){
            ew.eq("a.trade_type", orderType);
        }
        if(!Strings.isNullOrEmpty(status)){
            ew.eq("a.status", status);
        }
        if(!Strings.isNullOrEmpty(id)){
            ew.eq("a.id", id);
        }
        if(!Strings.isNullOrEmpty(userId)){
            ew.eq("a.user_id", userId);
        }
        if(!Strings.isNullOrEmpty(userName)){
            ew.where("b.username like  '%"+userName+"%'");
        }
        if(!Strings.isNullOrEmpty(mobile)){
            ew.where("b.mobile like  '%"+mobile+"%'");
        }
        if(!Strings.isNullOrEmpty(startTime)){
            ew.ge("a.created",startTime);
        }
        if(!Strings.isNullOrEmpty(endTime)){
            ew.le("a.created",endTime);
        }
        return ResultMap.getSuccessfulResult(entrustOrderService.selectListPage(page, ew));
    }

    @Log(value = "委托订单_创新交易导出",type = SysLogTypeEnum.EXPORT)
    @RequestMapping({"/exportList"})
    @ResponseBody
    @ApiOperation(authorizations = {@Authorization(value="Authorization")},value = "委托订单_创新交易订单导出", notes = "委托订单导出",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "marketId", value = "交易市场ID", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "交易方式(1买 2卖)", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "priceType", value = "价格类型(1 市价 2 限价)", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orderType", value = "订单类型(1 开仓 2 平仓)", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "交易状态(0未成交 1已成交 2已取消 4异常单)", required = false, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "订单ID", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userId", value = "用户ID", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "userName", value = "用户名", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "mobile", value = "手机号", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = false, dataType = "String", paramType = "query"),
    })
    public void exportList(@ApiIgnore Page<EntrustOrder> page,
                                   String marketId, String type,
                                   String priceType, String orderType,
                                   String status, String id,
                                   String userId, String userName,
                                   String mobile, String startTime,
                                   String endTime
                                    , HttpServletResponse response) throws Exception {
        EntityWrapper<EntrustOrder> ew = new EntityWrapper<>();
        ew.eq("market_type",2);
        if (!Strings.isNullOrEmpty(marketId)) {
            ew.eq("market_id", marketId);
        }
        if(!Strings.isNullOrEmpty(type)){
            ew.eq("a.type", type);
        }
        if(!Strings.isNullOrEmpty(priceType)){
            ew.eq("a.price_type", priceType);
        }
        if(!Strings.isNullOrEmpty(orderType)){
            ew.eq("a.trade_type", orderType);
        }
        if(!Strings.isNullOrEmpty(status)){
            ew.eq("a.status", status);
        }
        if(!Strings.isNullOrEmpty(id)){
            ew.eq("a.id", id);
        }
        if(!Strings.isNullOrEmpty(userId)){
            ew.eq("a.user_id", userId);
        }
        if(!Strings.isNullOrEmpty(userName)){
            ew.where("b.username like  '%"+userName+"%'");
        }
        if(!Strings.isNullOrEmpty(mobile)){
            ew.where("b.mobile like  '%"+mobile+"%'");
        }
        if(!Strings.isNullOrEmpty(startTime)){
            ew.ge("a.created",startTime);
        }
        if(!Strings.isNullOrEmpty(endTime)){
            ew.le("a.created",endTime);
        }
        page.setCurrent(1);
        page.setSize(100000);//限制导出数
        Page<EntrustOrder> retPage = entrustOrderService.selectListPage(page,ew);
        String[] header = {"订单ID", "用户ID","用户名", "交易市场",      "委托价格","委托数量", "交易方式","价格类型", "订单类型", "手续费","占用保证金","已成交量","委托时间","状态"};
        String[] properties = {"idStr", "userIdStr","userName", "marketName", "price", "volume", "typeStr", "priceTypeStr","tradeTypeStr","fee","lockMargin","deal","createdStr","statusStr"};


        CellProcessor[] PROCESSORS = new CellProcessor[] {
                null,
                null,
                null,
                null,
                new CellProcessorAdaptor() {
                    @Override
                    public String execute(Object value, CsvContext context) {
                        String v =String.valueOf(value);
                        if(value!=null){
                            DecimalFormat df=new DecimalFormat("0.00000000");
                            v=df.format(value);
                        }
                        return "\t"+v;
                    }
                },
                new CellProcessorAdaptor() {
                    @Override
                    public String execute(Object value, CsvContext context) {
                        String v =String.valueOf(value);
                        if(value!=null){
                            DecimalFormat df=new DecimalFormat("0.00000000");
                            v=df.format(value);
                        }
                        return "\t"+v;
                    }
                },
                null,
                null,
                null,
                new CellProcessorAdaptor() {
                    @Override
                    public String execute(Object value, CsvContext context) {
                        String v =String.valueOf(value);
                        if(value!=null){
                            DecimalFormat df=new DecimalFormat("0.00000000");
                            v=df.format(value);
                        }
                        return "\t"+v;
                    }
                },
                new CellProcessorAdaptor() {
                    @Override
                    public String execute(Object value, CsvContext context) {
                        String v =String.valueOf(value);
                        if(value!=null){
                            DecimalFormat df=new DecimalFormat("0.00000000");
                            v=df.format(value);
                        }
                        return "\t"+v;
                    }
                },
                new CellProcessorAdaptor() {
                    @Override
                    public String execute(Object value, CsvContext context) {
                        String v =String.valueOf(value);
                        if(value!=null){
                            DecimalFormat df=new DecimalFormat("0.00000000");
                            v=df.format(value);
                        }
                        return "\t"+v;
                    }
                },
                null,
                null
        };

        String fileName="创新交易委托.csv";
        try {
            ReportCsvUtils.reportListCsv(response,header,properties,fileName, retPage.getRecords(),PROCESSORS);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}

